<?php
class IndexModule
{
	public function index()
	{
		global $_FANWE;
		$cache_args = array(
			'index',
			CURRENT_CITY_PY,
			$_FANWE['page']
		);
		
		$cache_file = getTplCache('page/index_index',$cache_args);
		
		define('IS_CACHE_TPL',true);
		
		if(!@include($cache_file))
			IndexModule::getList();
		
		display($cache_file);
	}
	
	private function getList()
	{
		global $_FANWE;
		
		$site_list = $_FANWE['cache']['city_site_'.CURRENT_CITY_PY]['sites'];
		$site_count = count($site_list);
		
		if($site_count < $_FANWE['setting']['index_site_count'])
		{
			$site_all = $_FANWE['cache']['site']['all'];
			foreach($site_list as $site_id)
			{
				unset($site_all['s_'.$site_id]);
			}
			
			$site_all = array_slice($site_all,0,$_FANWE['setting']['index_site_count'] - $site_count);
			foreach($site_all as $site)
			{
				$site_list[] = $site['id'];
			}
			
			unset($site_all,$site,$site_count,$site_id);
		}
		elseif($site_count > $_FANWE['setting']['index_site_count'])
		{
			$site_list = array_slice($site_list,0,$_FANWE['setting']['index_site_count']);
		}
		
		if(!$no_query)
		{
			if(defined('IS_CACHE_TPL'))
				$fields = '';
			else
				$fields = ',g.post_count';
	
			$page_args = array(
				'city_py' => CURRENT_CITY_PY,
			);
			
			$insert_sort = 'gk.dc_sort ASC';
			
			$area_where = '';
			$city_name = $_FANWE['current_city']['name'];
			
			if(CURRENT_CITY_PY == CHINA_CITY_PY)
			{
				//全国不包含所有城市商品时
				if($_FANWE['setting']['all_city'] == 0)
				{
					$where.=  " AND gk.city = '$city_name'";
					$area_where .= " AND ga.city = '$city_name'";
				}
				else
					$insert_sort = 'gk.d_sort ASC';
			}
			elseif(CURRENT_CITY_PY != '')
			{
				//城市显示全国商品时
				if($_FANWE['setting']['index_all'] == 1)
				{
					$fields .= ",(gk.city = '$city_name') as is_now_city";
					$insert_sort = 'is_now_city DESC,'.$insert_sort;
					$quanguo_name = $_FANWE['cache']['city']['all'][CHINA_CITY_PY]['name'];
					$where.=  " AND gk.city IN ('$city_name','$quanguo_name')";
					$area_where .= " AND ga.city IN ('$city_name','$quanguo_name')";
				}
				else
				{
					$where.=  " AND gk.city = '$city_name'";
					$area_where .= " AND ga.city = '$city_name'";
				}
			}
			
			$goods_cates = S('Goods')->getGoodsNowCats($where);
				
			$order .= 'auction_sort DESC,'.$insert_sort.',gk.sort ASC,g.id ASC';
			
			$query_args = array();
			$query_args['is_auction'] = 1;
			$query_args['is_index'] = 1;
			
			$city_areas = S('Goods')->getGoodsNowArea($area_where.$where);
			
			$goods_result = S('Goods')->getGoodsNowList($where,$order,$fields,'',$query_args);
			$goods_result['ids'] = implode(',',$goods_result['ids']);
		}
		
		include template('page/index_index');
	}
}
?>